package com.jhy.guavacase.utilites;

import com.google.common.base.CharMatcher;
import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.nio.charset.Charset;

/**
 * @author jhy
 * @ClassName StringsCase.java
 * @Description Strings、工具类的使用
 * @createTime 2022年02月21日 13:04:00
 */
public class StringsCase {


    //1.Strings工具类的使用
    @Test
    public void StringsMethodsCase(){

        //(1)将空字符(Empty)转换位null
        //规则：空字符和null均转换为null，否则保持不变
        Assertions.assertEquals(Strings.emptyToNull(""),null);
        Assertions.assertEquals(Strings.emptyToNull("Hello Strings"),"Hello Strings");

        //(2)null转换为空字符(Empty)
        //规则：null会转换为空字符(Empty)，否则保持不变
        Assertions.assertEquals(Strings.nullToEmpty(null),"");
        Assertions.assertEquals(Strings.nullToEmpty("Hello Strings"),"Hello Strings");

        //(3)判断是否为null或者Empty
        Assertions.assertEquals(Strings.isNullOrEmpty(""),true);
        Assertions.assertEquals(Strings.isNullOrEmpty(null),true);

        //(3)两字符串的公共前缀
        Assertions.assertEquals(Strings.commonPrefix("Hello","HeStrings"),"He");

        //(4)两字符串的公共后缀
        Assertions.assertEquals(Strings.commonSuffix("Hello","Stringsllo"),"llo");

        //(5)字符串重复n次进行拼接
        Assertions.assertEquals(Strings.repeat("He",5),"HeHeHeHeHe");

        //(6)从开始位置补全字符串padStart（String string,int minLength,char padChar）
        //规则：如果字符串string长度小于指定长度minLength，则使用padChar填充到长度minLength(在string首部填充)
        //如果字符串string长度不小于指定长度minLength，则不做任何改变
        Assertions.assertEquals(Strings.padStart("Hello",7,'A'),"AAHello");
        Assertions.assertEquals(Strings.padStart("Hello",3,'A'),"Hello");

        //(7)从结尾位置补全字符串
        //规则：与padStart()类似，只是从尾部开始填充
        Assertions.assertEquals(Strings.padEnd("Hello",7,'A'),"HelloAA");
        Assertions.assertEquals(Strings.padEnd("Hello",3,'A'),"Hello");
    }

}
